/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2025-02-19 21:57:29
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2025-02-21 21:41:04
 * @FilePath: /thread/thread_concept/thread_test.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<iostream>
#include<pthread.h>
#include<unistd.h>



// void* threadStart(void *args)
// {
//     while(true)
//     {
//         sleep(1);
//         //std::cout<<" new thread is running... "<< " ,pid: "<<getpid()<< std::endl;
//         pthread_t tid=pthread_self();
//         std::cout<<" my tid = "<<tid<<std::endl;
//     }

// }

// int main()
// {
//     pthread_t tid1;
//     pthread_create(&tid1,nullptr,threadStart,(void *)"thread-new");

//     pthread_t tid2;
//     pthread_create(&tid2,nullptr,threadStart,(void *)"thread-new");

//     pthread_t tid3;
//     pthread_create(&tid3,nullptr,threadStart,(void *)"thread-new");

//     // 主线程
//     while(true)
//     {
//         std::cout<<" main is running..."<< " ,pid: "<< getpid()<<std::endl;
//         sleep(1);
//     }

//     return 0;
// }

// #include <stdio.h>
// #include <stdlib.h>
// #include <pthread.h>
// #include <unistd.h>
// #include <sys/types.h>

// void* Routine(void* arg)
// {
// 	char* msg = (char*)arg;
// 	int count = 0;
// 	while (count < 5){
// 		printf("I am %s...pid: %d, ppid: %d, tid: %lu\n", msg, getpid(), getppid(), pthread_self());
// 		sleep(1);
// 		count++;
// 	}
// 	return (void*)2022;
// }
// int main()
// {
// 	pthread_t tid[5];
// 	for (int i = 0; i < 5; i++){
// 		char* buffer = (char*)malloc(64);
// 		sprintf(buffer, "thread %d", i);
// 		pthread_create(&tid[i], NULL, Routine, buffer);
// 		printf("%s tid is %lu\n", buffer, tid[i]);
// 	}
// 	printf("I am main thread...pid: %d, ppid: %d, tid: %lu\n", getpid(), getppid(), pthread_self());
// 	for (int i = 0; i < 5; i++){
// 		void* ret = NULL;
// 		pthread_join(tid[i], &ret);
// 		printf("thread %d[%lu]...quit, exitcode: %d\n", i, tid[i], (int)ret);
// 	}
// 	return 0;
// }

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>

void* Routine(void* arg)
{
	char* msg = (char*)arg;
	int count = 0;
	while (count < 5){
		printf("I am %s...pid: %d, ppid: %d, tid: %lu\n", msg, getpid(), getppid(), pthread_self());
		sleep(1);
		count++;
		int a = 1 / 0; //error
	}
	return (void*)2022;
}
int main()
{
	pthread_t tid[5];
	for (int i = 0; i < 5; i++){
		char* buffer = (char*)malloc(64);
		sprintf(buffer, "thread %d", i);
		pthread_create(&tid[i], NULL, Routine, buffer);
		printf("%s tid is %lu\n", buffer, tid[i]);
	}
	printf("I am main thread...pid: %d, ppid: %d, tid: %lu\n", getpid(), getppid(), pthread_self());
	for (int i = 0; i < 5; i++){
		void* ret = NULL;
		pthread_join(tid[i], &ret);
		printf("thread %d[%lu]...quit, exitcode: %d\n", i, tid[i], (int)ret);
	}
	return 0;
}
